package com.example.task01_servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "CookieServlet3", value = "/cookie3")
public class CookieServlet3 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取客户端发来的Cookie信息并打印出来
        Cookie[] cookies = request.getCookies();
        for(Cookie tc:cookies){
            //2.当获取到的Cookie对象的名字为name时 将对应的数值设置为guanyu并添加到响应信息重
            if("name".equalsIgnoreCase(tc.getName())){
                tc.setValue("guanyu");
                response.addCookie(tc);
                break;
            }
        }
        System.out.println("修改Cookie信息成功!");
    }
}
